package com.abhimanyu.bootstrap.readers;

import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.abhimanyu.dataaccess.LocalTestUserDao;
import com.abhimanyu.dataaccess.model.User;

@Component
public class LocalDataSourceReader implements ItemStream, ItemReader<User> {

	@Autowired
	private LocalTestUserDao dao;
	private Iterator<User> usersIterator;
	Logger logger = Logger.getLogger(LocalDataSourceReader.class);
	public User read() throws Exception, UnexpectedInputException,
			ParseException, NonTransientResourceException {
		User user = null;
		if(usersIterator.hasNext()) {
			user = usersIterator.next();
		} 
		return user;
	}

	public void close() throws ItemStreamException {
		// TODO Auto-generated method stub

	}

	public void open(ExecutionContext arg0) throws ItemStreamException {
		
		logger.debug("EXECUTING");
		List<User> users= dao.fetchAll();
		logger.debug(users.size());
		logger.debug(users);
		usersIterator = users.iterator();
	}

	public void update(ExecutionContext arg0) throws ItemStreamException {
		// TODO Auto-generated method stub

	}
	
}
